using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using TaugeLibrary.Classes.Attributes;
using TaugeLibrary.Classes.Abstracts;
using System.Windows.Forms;
namespace TaugeLibrary.Classes
{
    public enum eViewMode
    {
        LIST = 1,
        QUERY = 2,
        INSERT = 4,
        UPDATE = 8,
        DELETE = 16,
        SEARCH = 32
        
    }

    public class LibConstants
    {

        public const string APPID = "GLAPP";

        public const string ENCRYPTION_KEY = "RANTAUPRAPATGL";

        public const string eVIEW = "VIEW";
        public const string eINSERT = "INSERT";
        public const string eUPDATE = "UPDATE";
        public const string eDELETE = "DELETE";
        public const string ePRINT = "PRINT";
        public const string ePOST = "POST";
        public const string eUNPOST = "UNPOST";

        public const string DBSYS = "dbsys";
        public const string DBDATA = "dbdata";

        public const string HEXCHAR = "0123456789ABCDEF";
        public const string CHARHEX_MIN = "ABCDEFGHIJKLMNO";
        public const string CHARHEX_EQUAL = "P";
        public const string CHARHEX_PLUS = "QRSTUVWXYZ12345";

        public const string META_DATA = "meta-data";

        public static string hrEncrypt(string target, string key)
        {
            string s1;
            string s2;
            string result;

            s1=target.Trim();
            s2=key;
            if (target.Length<key.Length)
                s1=target.PadRight(key.Length,' ');
            else
                s2=key.PadRight(target.Length,' ');

            s1=strToHex(s1);
            s2=strToHex(s2);

            result="";
            for(int i=s1.Length-1;i>=0;i--)
                result = result + hexSelisih(s1.Substring(i,1),s2.Substring(i,1));
            return result;
            
        }

        public static string hrDecrypt(string target, string key)
        {
            string s1;
            string s2;
            int nlen;
            string result;

            s1=target;
            s2=key;
            nlen=target.Length/2;
            if(key.Length<nlen)
                s2=key.PadRight(nlen,' ');
            else
                s1=target.PadLeft(key.Length *2,'P');

            s1=reverseString(s1);
            s2=strToHex(s2);

            result="";
            for(int i = 0 ;i<s1.Length;i++)
                result = result + hexTambah(s1.Substring(i,1),s2.Substring(i,1));

            return hexToStr(result).Trim();
        }              

        public static string hexToStr(string s)
        {
            string result="";
            for(int i =0;i<s.Length;i+=2)
                result=result + Convert.ToChar(hexToInt(s.Substring(i,2)));
            return result;
        }

        public static string strToHex(string s)
        {
            string result="";
            for(int i = 0;i<s.Length;i++)
                result = result + intToHex(Convert.ToInt32(Convert.ToChar(s.Substring(i,1))));
            return result;
        }

        public static string intToHex(int n)
        {
            int n1;
            int n2;
            n1=n/16;
            n2=n%16;
            return HEXCHAR.Substring(n1,1) + HEXCHAR.Substring(n2,1);
        }

        public static int hexToInt(string s)
        {
            return HEXCHAR.IndexOf(s.Substring(0,1)) * 16 + HEXCHAR.IndexOf(s.Substring(1,1));
        }

        public static string hexTambah(string c1,string c2)
        {
            int n1;
            int n2;

            if(c1=="P")
                return c2;
            else
            {
                n1=CHARHEX_MIN.IndexOf(c1) + 1;
                if(n1>0)
                    n1*=-1;
                else
                    n1=CHARHEX_PLUS.IndexOf(c1)+1;
            }

            n2=HEXCHAR.IndexOf(c2);

            if(n1+n2>15)
                return "";
            else
                return HEXCHAR.Substring(n1+n2,1);
        }

        public static string hexSelisih(string c1, string c2)
        {
            int n1;
            int n2;
            int n;

            n1=HEXCHAR.IndexOf(c1);
            n2=HEXCHAR.IndexOf(c2);

            n=n1-n2;
            if(n<0)
                return CHARHEX_MIN.Substring((n*-1) - 1,1);
            else if(n==0)
                return CHARHEX_EQUAL;
            else
                return CHARHEX_PLUS.Substring(n-1,1);

            
        }

        public static string reverseString(string s){        
            string result = "";
            for(int i = s.Length-1;i>=0;i--)
                result = result + s.Substring(i,1);
            return result;
        }

        
        public static int generateCheckSum(params string [] astr)
        {
            int nresult=0;
            string stmp;

            foreach(string str in astr)
            {
                for(int i=0 ;i<str.Length;i++)
                {
                    stmp=Convert.ToInt32(Convert.ToChar(str.Substring(i,1).ToUpper())).ToString();
                    for(int j=0;j<stmp.Length;j++)
                        nresult=nresult + Convert.ToInt32(stmp.Substring(j,1));
                }
            }
            return nresult;

        }

        public static string toSql(object o)
        {
            switch(o.GetType().Name)
            {
                case "String":
                case "string":
                    return "'" + o.ToString().Replace("'", "''") + "'";
                case "Int32":
                case "int":
                case "double":
                case "Double":
                    return o.ToString().Trim();
                case "Boolean":
                case "bool":
                    if ((bool)o)
                        return "1";
                    else
                        return "0";
                case "DateTime":
                    DateTime dt = (DateTime)o;
                    if(dt.Hour +dt.Minute +dt.Second+dt.Millisecond>0)
                        return "'" + ((DateTime)o).ToString("yyyy/MM/dd hh:mm:ss.fff") + "'";
                    else
                        return "'" + ((DateTime)o).ToString("yyyy/MM/dd")+"'";
                default:
                    if (o.GetType().IsEnum)
                        return ((int)o).ToString().Trim();
                    return toSql(o.ToString());                    
            }
        }
        public static object IsNull(object o, object value)
        {
            if(o==null)
                return value;
            else
                return o;
        }
        /*
        public static string toSql(string s)
        {
            return "'" + s.Replace("'", "''") + "'";
        }

        public static string toSql(int i)
        {
            return i.ToString().Trim();
        }

        public static string toSql(double i)
        {
            return i.ToString().Trim();
        }

        public static string toSql(bool b)
        {
            if (b)
                return "1";
            else
                return "0";
        }
        */
        public static string sqlStr(string s)
        {
            return "'" + s.Replace("'", "''") + "'";
        }
        

        public static string sqlNum(int i)
        {
            return i.ToString().Trim();
        }

        public static string sqlNum(double d)
        {
            if (d == 0)
                return "0";
            else
                return d.ToString("####.##").Trim();
        }

        public static string sqlBool(bool b)
        {
            if (b)
                return "1";
            else
                return "0";
        }

        public static string sqlDate(DateTime dt)
        {
            if (dt == DateTime.MinValue)
                return "NULL";
            else
                if(dt.Hour+dt.Minute+dt.Second+dt.Millisecond>0)                    
                    return LibConstants.sqlStr(dt.ToString("yyyy/MM/dd HH:mm:ss.fff"));
                else
                    return LibConstants.sqlStr(dt.ToString("yyyy/MM/dd"));
        }

        public static string txt2String(string spath) 
        {
            if(spath=="")
                return "";
            try{
                string result;
                StreamReader sr = new StreamReader(spath);
                result = sr.ReadToEnd();
                sr.Close();
                sr=null;
                return result;
            }catch{
                return "";
            }
        }

        
        public static string toStr(object o)
        {
            return o.ToString();
        }
        public static int toInt(object o)
        {
            return Convert.ToInt32(o);
        }
        public static double toDbl(object o)
        {
            return Convert.ToDouble(o);
        }
        public static bool toBool(object o)
        {
            return Convert.ToBoolean(o);
        }
        public static DateTime toDate(object o)
        {
            return Convert.ToDateTime(o);
        }

        public static TaugeDateTime toTaugeDateTime(object o)
        {
            if (o == null||o is DBNull)
                return new TaugeDateTime();
            else
                return new TaugeDateTime(Convert.ToDateTime(o));
            
        }

        public static object[] getAttributes(System.Reflection.PropertyInfo p_target_type, Type p_attribute_type)
        {
            System.Reflection.PropertyInfo inf = p_target_type;
            object[] attributes = inf.GetCustomAttributes(p_attribute_type, false);
            if (attributes.Length > 0)
                return attributes;
            else
                return null;
        }

        public static object getAttribute(System.Reflection.PropertyInfo p_target_type, Type p_attribute_type)
        {
            object[] att = getAttributes(p_target_type, p_attribute_type);
            if (att == null)
                return null;
            else
                return att[att.Length - 1];
        }



        public static object[] getAttributes(System.Reflection.MemberInfo p_target_type, Type p_attribute_type)
        {
            System.Reflection.MemberInfo inf = p_target_type;
            object[] attributes = inf.GetCustomAttributes(p_attribute_type, false);
            if (attributes.Length > 0)
                return attributes;
            else
                return null;
        }

        public static object getAttribute(System.Reflection.MemberInfo p_target_type, Type p_attribute_type)
        {
            object[] att = getAttributes(p_target_type, p_attribute_type);
            if (att == null)
                return null;
            else
                return att[att.Length - 1];
        }


        public static string[] getAttArr(System.Reflection.MemberInfo p_target_type, string p_arrname)
        {
            AttArr[] arr = (AttArr[])LibConstants.getAttributes(p_target_type, typeof(AttArr));
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i].ArrName == p_arrname)
                {
                    return arr[i].StrItems;
                }
            }
            return null;
        }

        /*PENDING
        public static object Lookup(System.Windows.Forms.IWin32Window f, LookupParameter lp, object p_default_val)
        {
            return Lookup(f, lp.Sql, lp.ReturnColumn, p_default_val, lp.LookupColumn, lp.Title);
        }


        
        public static object Lookup(System.Windows.Forms.IWin32Window f, string p_sql, string p_returncolumn, object p_defaultvalue, string p_default_column_search, string p_title)
        {
            object[] tmp = Constants.Lookup(f,p_sql,new string[]{p_returncolumn},new object[]{p_defaultvalue}, p_default_column_search,p_title);
            
            if (tmp!=null)
            {
                return tmp[0];
            }
            else
            {
                return p_defaultvalue;
            }            
        }

        
        public static object[] Lookup(System.Windows.Forms.IWin32Window f, string p_sql, string[] p_returncolumns, object[] p_defaultvalues, string p_default_column_search,string p_title)
        {
            frmLookup fl = new frmLookup(p_sql,p_returncolumns,p_default_column_search,p_title,Program.db);
            fl.ShowDialog(f);
            if (fl.Values!=null)
            {
                return fl.Values;
            }
            else
            {
                return p_defaultvalues;
            }
            
        }
        

        public static object Lookup(System.Windows.Forms.IWin32Window f, LookupParameter lp, object p_default_val,Database p_db)
        {
            return Lookup(f, lp.Sql, lp.ReturnColumn, p_default_val, lp.LookupColumn, lp.Title,p_db);
        }



        public static object Lookup(System.Windows.Forms.IWin32Window f, string p_sql, string p_returncolumn, object p_defaultvalue, string p_default_column_search, string p_title, Database p_db)
        {
            object[] tmp = Constants.Lookup(f, p_sql, new string[] { p_returncolumn }, new object[] { p_defaultvalue }, p_default_column_search, p_title,p_db);

            if (tmp != null)
            {
                return tmp[0];
            }
            else
            {
                return p_defaultvalue;
            }
        }


        public static object[] Lookup(System.Windows.Forms.IWin32Window f, string p_sql, string[] p_returncolumns, object[] p_defaultvalues, string p_default_column_search, string p_title, Database p_db)
        {
            frmLookup fl = new frmLookup(p_sql, p_returncolumns, p_default_column_search, p_title,p_db);
            fl.ShowDialog(f);
            if (fl.Values != null)
            {
                return fl.Values;
            }
            else
            {
                return p_defaultvalues;
            }

        }
        */

        public static string DateToText (DateTime p_dt)
        {
            
            if (p_dt == DateTime.MinValue)
                return "";
            else if (p_dt.Hour + p_dt.Minute + p_dt.Second + p_dt.Millisecond > 0)
                return p_dt.ToString("dd/MM/yyyy hh:mm:ss"); //PENDING return p_dt.ToString(Program.FORMAT_DATETIME);
            else
                return p_dt.ToString("dd/MM/yyyy"); //PENDING return p_dt.ToString(Program.FORMAT_DATE);
             
        }

        public static string ObjectToString(object o)
        {
            try
            {
                return o.ToString();
            }
            catch { return ""; }
        }

        public static void TidyGrid(DataGridView dgData)
        {
            foreach (DataGridViewColumn cl in dgData.Columns)
            {
                string m_type = cl.ValueType.ToString();
                if (m_type == "System.String")
                {
                }
                else if (m_type == "System.DateTime")
                {
                    cl.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                    cl.DefaultCellStyle.Format = "dd/MM/yyyy";
                    if (dgData.Rows.Count > 0)
                    {
                        if (!(dgData[cl.Name, 0].Value is DBNull))
                        {
                            DateTime dt = Convert.ToDateTime(dgData[cl.Name, 0].Value);
                            if (dt.Hour + dt.Minute + dt.Second + dt.Millisecond > 0)
                            {
                                cl.DefaultCellStyle.Format = "dd/MM/yyyy     hh:mm:ss.fff";
                            }
                        }
                    }
                }
                else if (m_type == "System.Int32" || m_type == "System.Integer")
                {
                    cl.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                    cl.DefaultCellStyle.Format = "#,###";
                }
                else if (m_type == "System.Decimal")
                {
                    cl.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                    cl.DefaultCellStyle.Format = "#,###.##";
                }
                else if (m_type == "System.Boolean")
                {

                }
                else
                {
                    cl.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                    cl.DefaultCellStyle.Format = "";
                }
            }
        }

        public static Control getControl(ContainerControl f, string p_controlname)
        {
            Control[] _ctlarr = f.Controls.Find(p_controlname, true);
            Control _ctl = null;
            if (_ctlarr != null && _ctlarr.Length > 0)
                _ctl = _ctlarr[_ctlarr.Length - 1];
            return _ctl;
        }


        


    }
}
